using System;
using System.Globalization;
using System.IO;
using System.Xml;

namespace HabilisDesign.Iota
{
    public class Status
    {
        #region Fields

        private string m_Id;
        private DateTime m_CreatedAt;
        private string m_Text;
        private User m_User;

        #endregion

        #region Properties

		/// <summary>
		/// Gets the id.
		/// </summary>
		/// <value>The id.</value>
        public string Id
        {
            get
            {
                return m_Id;
            }
			
        }

		/// <summary>
		/// Gets the created at.
		/// </summary>
		/// <value>The created at.</value>
        public DateTime CreatedAt
        {
            get
            {
                return m_CreatedAt;
            }
			
        }

		/// <summary>
		/// Gets the create time text.
		/// </summary>
		/// <value>The create time text.</value>
		public string CreateTimeText
		{
			get
			{
				return m_CreatedAt.ToShortTimeString();
			}
		}

		/// <summary>
		/// Gets the text.
		/// </summary>
		/// <value>The text.</value>
        public string Text
        {
            get
            {
                return m_Text;
            }
			
        }

		/// <summary>
		/// Gets the user.
		/// </summary>
		/// <value>The user.</value>
        public User User
        {
            get
            {
                return m_User;
            }
			
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the Status class.
        /// </summary>
        private Status()
        {
        }

        #endregion

		/// <summary>
		/// Parses the specified XML fragment.
		/// </summary>
		/// <param name="xmlFragment">The XML fragment.</param>
		public static Status Parse( string xmlFragment )
		{
			Status status = new Status();

			XmlReaderSettings settings = new XmlReaderSettings();
			settings.IgnoreProcessingInstructions = true;
			settings.IgnoreWhitespace = true;
			settings.IgnoreComments = true;
			settings.ConformanceLevel = ConformanceLevel.Fragment;
			settings.CloseInput = true;
			using ( XmlReader reader = XmlReader.Create( new StringReader( xmlFragment ), settings ) )
			{
				while ( reader.Read() )
				{
					if ( reader.IsStartElement( "created_at" ) )
					{
						reader.Read();
						status.m_CreatedAt = DateTime.ParseExact( reader.Value, "ddd MMM dd HH:mm:ss zzzzz yyyy", CultureInfo.InvariantCulture );
					}
					if ( reader.IsStartElement( "id" ) && string.IsNullOrEmpty( status.m_Id ) )
					{
						reader.Read();
						status.m_Id = reader.Value;
					}
					if ( reader.IsStartElement( "text" ) )
					{
						reader.Read();
						status.m_Text = reader.Value;
					}
					if ( reader.IsStartElement( "user" ) )
					{
						status.m_User = User.Parse( reader.ReadInnerXml() );
					}
				}
				reader.Close();
			}
			return status;
		}

    } // end class

} // end namespace
